package com.student_exam_system.service.impl;

import com.student_exam_system.Util.MybatisUtils;
import com.student_exam_system.dao.QuestionMapper;
import com.student_exam_system.pojo.Question;
import com.student_exam_system.service.QuestionService;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import java.util.List;
/**
 * @author fire'ge
 */
@Service("questionServiceImpl")
public class QuestionServiceImpl implements QuestionService {
final
QuestionMapper questionMapper;

    public QuestionServiceImpl(QuestionMapper questionMapper) {
        this.questionMapper = questionMapper;
    }

    public QuestionMapper getQuestionMapper() {
        return questionMapper;
    }

    @Override
    public List<Question> queryQuestionsByPaperId(int paperId) {
        return questionMapper.queryQuestionsByPaperId(paperId);
    }
    private final SqlSession session = MybatisUtils.getSession();
    @Override
    public int addQuestion(String content, String type, String answer, String score) {
        if (content == null || type == null || answer == null || score == null) {
            return 0;
        }
        int i = 0;
        try {

            double point = Double.parseDouble(score);
            Question question = new Question(0, content, type, answer, point);
            i = questionMapper.insertQuestion(question);
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            MybatisUtils.closeSession(session);
        }
        return i;
    }
}
